package com.shanjupay.merchant.controller;

import com.shanjupay.merchant.convert.MerchantConvert;
import com.shanjupay.merchant.servicve.SmsCodeService;
import com.shanjupay.merchant.vo.MerchantVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import shanjupay.merchant.Dto.MerchantDTO;
import shanjupay.merchant.api.MerchantService;


@Api(value = "商户平台‐商户相关", tags = "商户平台‐商户相关", description = "商户平台‐商户相关")
@RestController
public class MerchantController {

    @Reference
    private MerchantService merchantService;

    @Autowired
    private SmsCodeService smsCodeService;


    @ApiOperation("测试接口")
    @GetMapping("/merchants/{id}")
    public MerchantDTO queryMerchantById(@PathVariable("id") Long id) {
        MerchantDTO merchantDTO = merchantService.queryMerchantById(id);
        return merchantDTO;
    }


    /**
     * 发送验证码
     *
     * @param phone 手机号
     * @return
     */
    @ApiOperation("获取手机验证码")
    @ApiImplicitParam(value = "手机号", name = "手机号",
            required = true, dataType = "String", paramType = "query")
    @GetMapping("/sms")
    public String getSmsCode(@RequestParam("phone") String phone) {
        return smsCodeService.getSmsCode(phone);
    }

    /**
     * 商户注册
     *
     * @param merchantVo
     * @return
     */
    @ApiOperation("注册商户")
    @ApiImplicitParam(name = "merchantRegister", value = "注册信息", required = true, dataType =
            "MerchantRegisterVO", paramType = "body")
    @PostMapping("/merchants/register")
    public MerchantVo registerMerchant(@RequestBody MerchantVo merchantVo) {
        //校验验证码
        smsCodeService.checkVerifiyCode(merchantVo.getVerifiykey(), merchantVo.getVerifiyCode());
        //注册商户
//        MerchantDTO merchantDTO = new MerchantDTO();
//        merchantDTO.setMobile(merchantVo.getMobile());
//        merchantDTO.setUsername(merchantVo.getUsername());
//        merchantDTO.setPassword(merchantVo.getPassword());

        //vo --> dto
        MerchantDTO merchantDTO = MerchantConvert.INSTANCE.MerchantVo2MercahntDto(merchantVo);
        //注册商户
        MerchantDTO merchant = merchantService.createMerchant(merchantDTO);
        //dto --> vo
        MerchantVo merchantVo1 = MerchantConvert.INSTANCE.MerchantDto2MerchantVo(merchant);
        return merchantVo1;
    }
}
